package com.itheima.day07.Iterator.test1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class Test {
    public static void main(String[] args) {
        Collection<Integer> collection = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            collection.add(Integer.valueOf(i));
        }
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        System.out.println(list.get(0));


//        通过迭代器遍历Collection集合的步骤
//        1.通过集合对象获取迭代器对象
        Iterator<Integer> iterator = collection.iterator();

//        2.通过hasNext()方法循环判断迭代器对象中是否还有元素
        System.out.println(iterator.hasNext());

//        3.如果有元素,通过next()方法获取迭代器中的元素
        /*System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());

        try{
            System.out.println(iterator.next()); // NoSuchElementException
        } catch (NoSuchElementException e){
            System.out.println("迭代器遍历Collection集合越界");
        }*/

        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }


    }
}
